////////////////////////////////////////////////////////////////////////////////////
//
// file : ProcIntro.js
//
// proj : Tring
// auth : instil
// date : 110810
// desc : Game intro procedure
//
////////////////////////////////////////////////////////////////////////////////////

// Time State
var TIME_READY : float = 4.0f;
var TIME_FIRE : float = 5.0f;
var TIME_GO : float = 7.0f;
var TIME_FLY : float = 9.0f;
var TIME_START : float = 12.5f;

var STATE_NONE : int = 0;
var STATE_READY : int = 1;
var STATE_FIRE : int = 2;
var STATE_GO : int = 3;
var STATE_FLY : int = 4;
var STATE_START : int = 5;

// Force
var FORCE_FIRE : float = 25.0f;
var FORCE_FLY : float = 50.0f;

// Objects
var particleStart : ParticleEmitter;		// [PARTICLE]	Rocket explosion
var particleFly : ParticleEmitter;			// [PARTICLE]	Rocket fly
var audioFire : AudioSource;				// [AUDIO]		Rocket fire
var audioTransition : AudioSource;			// [AUDIO]		Transition sound
var textSkip : GUIText;						// [GUITEXT]	Skip intro
var textureTrans : GUITexture;				// [GUITEXTURE]	Transition

private var g_vecStartPostion : Vector3;
private var ROCKET_SHAKE_RANGE : float = 0.5f;
private var CAMERA_MOVE : Vector3 = Vector3( 20.0f, 10.0f, 20.0f );
private var CAMERA_MOVE2 : Vector3 = Vector3( 10.0f, 10.0f, 10.0f );
private var g_bStart : boolean = false;
private var g_nState : int = STATE_NONE;

private var g_fStartedTime : float;

function Start()
{
	// Transition
	textureTrans.color.a = 1.0f;
	g_fStartedTime = Time.time;
	g_vecStartPostion = transform.position;
	Camera.mainCamera.transform.LookAt( transform.position );
}

function Update ()
{
	if( g_bStart )
	{
		// Gamy play start
		textureTrans.color.a += Time.deltaTime;
		textureTrans.color.r -= Time.deltaTime;
		textureTrans.color.g -= Time.deltaTime;
		textureTrans.color.b -= Time.deltaTime;
		
		if( textureTrans.color.a >= 1.0f )
			Application.LoadLevel("GamePlay");
	}
	else
	{
		// Intro start
		if( textureTrans.color.a > 0.0f )
		textureTrans.color.a -= Time.deltaTime;
	}
	    
	if( Time.time - g_fStartedTime > TIME_START )
	{
		g_nState = STATE_START;
	}
	else if( Time.time - g_fStartedTime > TIME_FLY )
	{
		g_nState = STATE_FLY;
	}
	else if( Time.time - g_fStartedTime > TIME_GO )
	{
		g_nState = STATE_GO;
	}
	else if( Time.time - g_fStartedTime > TIME_FIRE )
	{
		g_nState = STATE_FIRE;
	}
	else if( Time.time - g_fStartedTime > TIME_READY )
	{
		g_nState = STATE_READY;
	}
	
	// Move Left
    if ((Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2")) &&
    	textSkip.enabled == true)
    {
    	audioTransition.Play();
    	GameStart();
    }
    
    switch( g_nState )
    {
    case STATE_START:
		GameStart();
    	break;
	case STATE_FLY:
		rigidbody.AddForce( 0.0f, FORCE_FLY, 0.0f );
		particleStart.emit = false;
		particleFly.emit = true;
		
		Camera.mainCamera.transform.position += (CAMERA_MOVE * Time.deltaTime);
		Camera.mainCamera.transform.LookAt( transform.position );
		
		if( audioFire.volume > 0.1f )
			audioFire.volume -= Time.deltaTime;
		break;
	case STATE_GO:
		Camera.mainCamera.transform.LookAt( transform.position );
		rigidbody.AddForce( 0.0f, FORCE_FIRE, 0.0f );
		ShakeRocket(true);
		break;
	case STATE_FIRE:
		ShakeRocket(false);
		break;
	case STATE_READY:
		particleStart.emit = true;
		
		textSkip.enabled = true;
		break;
	default:
		Camera.mainCamera.transform.position -= (CAMERA_MOVE2 * Time.deltaTime );
		Camera.mainCamera.transform.LookAt( transform.position );
		break;
    }
}

function ShakeRocket( bMove : boolean )
{
	var vDelta : Vector3 = Vector3( Random.value * ROCKET_SHAKE_RANGE * 2.0f - ROCKET_SHAKE_RANGE,
									Random.value * ROCKET_SHAKE_RANGE * 2.0f - ROCKET_SHAKE_RANGE,
									Random.value * ROCKET_SHAKE_RANGE * 2.0f - ROCKET_SHAKE_RANGE );

	if( bMove )
	{
		vDelta.y = 0.0f;
		transform.position += vDelta;
	}
	else
		transform.position = g_vecStartPostion + vDelta;
}

function GameStart()
{
	if( g_bStart == false )
	{
		g_bStart = true;
		textSkip.enabled = false;
	}
}